// store命名
import { StoreName } from '@store/store-name';
// api
import { login, register, refreshToken } from '@api/user';
// cookie
import { removeToken } from '@utils/cookie';
import { TokenKey } from '@utils/cookie/tokenKey';
// 引入element-ui 的message组件
import { ElMessage } from 'element-plus';
import { usePermissionStore } from './permission';

// 用户模块
export const useUserStore = defineStore(StoreName.USER, {
    state: (): UserStore => ({
        id: '',
        email: '',
        username: '',
        avatar: '',
        roles: [],
        permissions: [],
    }),
    actions: {
        // 注册
        register(registerForm: AjaxRequest['registerForm']) {
            return new Promise((resolve, _reject) => {
                register(registerForm).then((res) => {
                    // 成功注册提示
                    ElMessage({
                        message: res.message,
                        type: 'success',
                    });
                    // 缓存当前登录数据
                    this.id = res.data.id || '';
                    this.email = res.data.email || '';
                    this.username = res.data.username || '';
                    this.avatar = res.data.avatar || '';
                    this.roles = res.data.roles || '';
                    this.permissions = res.data.permissions || '';
                    // 成功回调
                    resolve(true);
                });
            });
        },
        // 登录
        login(loginForm: AjaxRequest['loginForm']) {
            return new Promise((resolve, _reject) => {
                login(loginForm).then((res) => {
                    // 成功登录提示
                    ElMessage({
                        message: res.message,
                        type: 'success',
                    });
                    // 缓存当前登录数据
                    this.id = res.data.id || '';
                    this.email = res.data.email || '';
                    this.username = res.data.username || '';
                    this.avatar = res.data.avatar || '';
                    this.roles = res.data.roles || '';
                    this.permissions = res.data.permissions || '';
                    // 成功回调
                    resolve(true);
                });
            });
        },
        // 无感登录
        loginNoSense() {
            return new Promise((resolve) => {
                // 发送请求
                refreshToken().then(() => {
                    resolve(true);
                });
            });
        },
        // 权限验证
        checkPermission(permission: string): Boolean {
            if (this.permissions.some((_permission) => _permission === permission) || this.roles.some((_role) => _role.role == 'root')) {
                return true;
            } else {
                ElMessage({
                    message: '您没有权限访问该操作',
                    type: 'warning',
                });
                return false;
            }
        },
        // 退出登录
        logout() {
            return new Promise((resolve, _reject) => {
                // 清除当前登录数据
                this.id = '';
                this.email = '';
                this.username = '';
                this.avatar = '';
                this.roles = [];
                this.permissions = [];
                // 清除token
                removeToken(TokenKey.online);
                // 清除路由
                usePermissionStore().clearRoute();
                // 成功回调
                resolve(true);
            });
        },
    },
    persist: {
        storage: sessionStorage,
    },
});
